using System.Globalization;
using Engine;
using Engine.Graphics;
using Game;

namespace SCIENEW {
    public class RodItem : MeshItem, IMaterialBlock {
        public Materials Material;
        public Materials GetMaterial() => Material;
        public RodItem(Materials type) : this(type + "Rod", type == Materials.Copper ? Engine.Color.White : MaterialsUtils.GetColor(type), type == Materials.Copper) {
            Material = type;
            string name = type.ToDisplayName();
            DisplayName = string.Format(LanguageControl.Get("Items", "Names", "RodItem"), name);
            Description = string.Format(LanguageControl.Get("Items", "Descriptions", "RodItem"), name);
        }

        public RodItem(string id, Color color, bool isCopper = false) : base(id) {
            Material = Materials.None;
            Id = id;
            m_standaloneBlockMesh.AppendMesh("Models/Rods", "SteelRod", Matrix.CreateTranslation(0, -0.5f, 0), Matrix.Identity, color);

            Matrix tcTransform = isCopper ? Matrix.CreateTranslation(3 / 16f, 1 / 16f, 0) : Matrix.CreateTranslation(3 / 16f, 0, 0);//是否为铜，是则用铜材质
            m_standaloneBlockMesh.TransformTextureCoordinates(tcTransform);
        }

        public override Vector3 GetIconViewOffset(int value, DrawBlockEnvironmentData environmentData) => new Vector3(-1, 0.5f, 0);

        public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {
            BlocksManager.DrawMeshBlock(primitivesRenderer, m_standaloneBlockMesh, IndustrialModLoader.IETexture, color, 1.6f * size, ref matrix, environmentData);
        }

        public override float GetMeleePower(int value) => 2f;

        public override Vector3 GetIconBlockOffset(int value, DrawBlockEnvironmentData environmentData) => Vector3.Zero;

        public override float GetIconViewScale(int value, DrawBlockEnvironmentData environmentData) => 0.85f;

        public override CraftingRecipe[] GenerateProceduralRecipe(int value) {
            //锭到棍
            if (Material == Materials.None) return base.GenerateProceduralRecipe(value);
            CraftingRecipe craftingRecipe = new CraftingRecipe {
                Description = $"将{Material.ToDisplayName()}锭锻造成棒",
                Ingredients = new string[36],
                RemainsValue = 0,
                RemainsCount = 0,
                RequiredHeatLevel = MaterialsUtils.GetRequiredFuelLevel(Material),
                RequiredPlayerLevel = 0,
                ResultValue = ItemsBlockManager.GetItemID(this),
                ResultCount = 1,
            };
            craftingRecipe.Ingredients[0] = craftingRecipe.Ingredients[1] = IECraftingRecipesManager.BlockToIngredient(ItemsBlockManager.GetItemIdByMaterial<MetalIngotItem>(Material));
            //锭到棍（压榨机）
            CraftingRecipe craftingRecipe2 = new CraftingRecipe {
                Description = $"用压榨机将{Material.ToDisplayName()}锭挤压成棒",
                Ingredients = new string[36],
                RemainsValue = 0,
                RemainsCount = 0,
                RequiredHeatLevel = ComponentSqueezer.RequiredHeatLevel,
                RequiredPlayerLevel = 0,
                ResultValue = ItemsBlockManager.GetItemID(this),
                ResultCount = 2,
            };
            craftingRecipe2.Ingredients[0] = IECraftingRecipesManager.BlockToIngredient(ItemsBlockManager.GetItemIdByMaterial<MetalIngotItem>(Material));
            return [craftingRecipe, craftingRecipe2];
        }
    }
}